package com.zbkj.service.model.front.vo.user;

import com.zbkj.dal.common.utils.MoneyUtil;
import com.zbkj.dal.entity.system.SystemUserLevel;
import com.zbkj.service.model.front.vo.fund.UserQuotaVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

@Data
@ApiModel
@EqualsAndHashCode(callSuper = true)
public class UserQuotaInfoVO extends UserQuotaVO {

    @ApiModelProperty(value = "下一等级", example = "8")
    private Integer nextLevel;

    @ApiModelProperty(value = "距下一等级需要额度", example = "800000.00")
    private String nextLevelQuota;

    @ApiModelProperty(value = "下一级进度", example = "0.80")
    private BigDecimal nextLevelProgress;

    @ApiModelProperty(value = "已用进度", example = "0.30")
    private BigDecimal usedProgress;

    @ApiModelProperty(value = "当前等级最低额度", example = "120000")
    private String minQuota;

    @ApiModelProperty(value = "当前等级最高额度", example = "200000")
    private String maxQuota;

    public static UserQuotaInfoVO generate(UserQuotaVO userQuotaVO, SystemUserLevel systemUserLevel) {
        UserQuotaInfoVO userQuotaInfoVO = new UserQuotaInfoVO();
        userQuotaInfoVO.setLevelDesc(userQuotaVO.getLevelDesc());
        userQuotaInfoVO.setLevelId(userQuotaVO.getLevelId());
        userQuotaInfoVO.setMemberLevel(userQuotaVO.getMemberLevel());
        userQuotaInfoVO.setTotalQuota(userQuotaVO.getTotalQuota());
        userQuotaInfoVO.setUsedQuota(userQuotaVO.getUsedQuota());
        userQuotaInfoVO.setAvailableQuota(userQuotaVO.getAvailableQuota());
        if (Objects.equals(systemUserLevel.getId(), userQuotaVO.getMemberLevel())) {
            BigDecimal usedProgress = userQuotaVO.getUsedQuotaOriginal().multiply(BigDecimal.valueOf(100)).divide(userQuotaVO.getTotalQuotaOriginal(), 2, RoundingMode.DOWN);
            userQuotaInfoVO.setNextLevelProgress(BigDecimal.valueOf(100).setScale(2, RoundingMode.DOWN));
            userQuotaInfoVO.setUsedProgress(usedProgress);
            userQuotaInfoVO.setNextLevel(systemUserLevel.getGrade());
            userQuotaInfoVO.setNextLevelQuota(MoneyUtil.prettyFormat(BigDecimal.ZERO));
        } else {
            BigDecimal nextLevelProgress = systemUserLevel.getLimitDown().compareTo(userQuotaVO.getTotalQuotaOriginal()) <= 0 ? BigDecimal.valueOf(100) :
                    userQuotaVO.getTotalQuotaOriginal().multiply(BigDecimal.valueOf(100)).divide(systemUserLevel.getLimitDown(), 2, RoundingMode.DOWN);
            BigDecimal usedProgress = userQuotaVO.getUsedQuotaOriginal().multiply(BigDecimal.valueOf(100)).divide(systemUserLevel.getLimitDown(), 2, RoundingMode.DOWN);
            userQuotaInfoVO.setNextLevelProgress(nextLevelProgress);
            userQuotaInfoVO.setUsedProgress(usedProgress);
            userQuotaInfoVO.setNextLevel(Optional.of(systemUserLevel).map(SystemUserLevel::getGrade).orElse(userQuotaVO.getMemberLevel()));
            String nextLevelGap = Optional.of(systemUserLevel).map(level -> level.getLimitDown().subtract(userQuotaVO.getTotalQuotaOriginal()))
                    .map(quota -> quota.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : quota).map(MoneyUtil::prettyFormat).orElse("0.00");
            userQuotaInfoVO.setNextLevelQuota(nextLevelGap);
        }
        userQuotaInfoVO.setMinQuota(MoneyUtil.prettyFormat(userQuotaVO.getLimitDown()));
        userQuotaInfoVO.setMaxQuota(MoneyUtil.prettyFormat(userQuotaVO.getLimitUp()));
        userQuotaInfoVO.setBackImage(userQuotaVO.getBackImage());
        return userQuotaInfoVO;
    }
}
